package com.star.message.biz;

import com.star.common.core.contract.Page;
import com.star.common.core.utils.BeanKit;
import com.star.common.sms.contract.command.SmsTemplateAddCommand;
import com.star.common.sms.contract.command.SmsTemplateUpdateCommand;
import com.star.common.sms.contract.enums.TemplateStatusEnum;
import com.star.common.sms.contract.exception.SmsException;
import com.star.common.sms.contract.query.SmsTemplatePageQuery;
import com.star.common.sms.contract.vo.SmsTemplatePageVO;
import com.star.common.sms.contract.vo.SmsTemplateQueryVO;
import com.star.common.sms.factory.SmsFactory;
import com.star.message.contract.command.SmsTemplateDeleteCommand;
import com.star.message.contract.enums.MessageErrorEnum;
import com.star.message.contract.param.UserSmsConfigParam;
import com.star.message.contract.query.TemplatePageQuery;
import com.star.message.repository.model.UserSmsConfigBO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 短信模板服务
 *
 * @author zhouhengzhe
 */
@Service
@RequiredArgsConstructor
public class SmsTemplateBizService {

    private final SmsFactory smsFactory;

    /**
     * 查询短信模板
     *
     * @param req 分页查询参数
     * @return 短信模板分页数据
     */
    public Page<SmsTemplatePageVO> pageSmsTemplate(TemplatePageQuery req) {
        return smsFactory.getAliyunSmsClient().pageSmsTemplate(
                SmsTemplatePageQuery
                        .builder()
                        .current(req.getCurrent())
                        .size(req.getSize())
                        .ownerId(req.getOwnerId())
                        .resourceOwnerAccount(req.getResourceOwnerAccount())
                        .resourceOwnerId(req.getResourceOwnerId())
                        .build()
        );
    }

    /**
     * 添加短信模板
     */
    public String addSmsTemplate(SmsTemplateAddCommand command) {
        return smsFactory.getAliyunSmsClient().addSmsTemplate(
                BeanKit.copy(command, SmsTemplateAddCommand.class)
        );
    }

    /**
     * 删除短信模板
     *
     * @param smsTemplateDeleteCommand 模板code
     * @return true/false 成功/失败
     */
    public Boolean deleteSmsTemplate(SmsTemplateDeleteCommand smsTemplateDeleteCommand) {
        Boolean deleteSmsTemplateFlag = smsFactory.getAliyunSmsClient()
                .deleteSmsTemplate(smsTemplateDeleteCommand.getTemplateCode());
        if (Boolean.TRUE.equals(deleteSmsTemplateFlag)) {
            //删除短信正在使用的模版记录
            UserSmsConfigBO.repository().delete(UserSmsConfigParam.builder().templateCode(smsTemplateDeleteCommand.getTemplateCode()).build());
        }
        return Boolean.TRUE;
    }

    /**
     * 修改未审核通过的短信模板信息，并重新提交审核
     *
     * @param command 模板信息
     */
    public Boolean modifyAndReSubmitSmsTemplate(SmsTemplateUpdateCommand command) {
        SmsTemplateQueryVO smsTemplateQueryVO = smsFactory.getAliyunSmsClient()
                .querySmsTemplateAuditStatus(command.getTemplateCode());
        if (TemplateStatusEnum.REJECT.getCode().equals(smsTemplateQueryVO.getTemplateStatus())) {
            return smsFactory.getAliyunSmsClient()
                    .modifyAndReSubmitSmsTemplate(command);
        }
        throw new SmsException(MessageErrorEnum.TEMPLATE_STATUS_NOT_FAIL);
    }
}
